package org.xmap.util;
public final class ByteOrder {
    /**
     * 表示字节为小尾，低字节在前，高字节在后。
     */
    public static final boolean LITTLE_ENDIAN = false;
    /**
     * 表示字节为大尾，高字节在前，低字节在后。
     */
    public static final boolean BIG_ENDIAN = true;
    /**
     * convert between big endian and small endian
     * @param from short value to be converted
     * @return short result
     */
    public static short convert(short from) {
        int byte0 = from & 0x000000ff;
        int byte1 = ((from & 0x0000ff00) >>> 8);
        return (short) ((byte0 << 8) + byte1);
    }
    /**
     * convert between big endian and small endian
     * @param from int value to be converted
     * @return short result
     */
    public static int convert(int from) {
        int byte0 = from & 0x000000ff;
        int byte1 = ((from & 0x0000ff00) >>> 8);
        int byte2 = ((from & 0x00ff0000) >>> 16);
        int byte3 = ((from & 0xff000000) >>> 24);
        return (int) ((byte0 << 24) + (byte1 << 16) + (byte2 << 8) + byte3);
    }
    /**
     * convert between big endian and small endian
     * @param from long value to be converted
     * @return short result
     */
    public static long convert(long from) {
        int byte0 = (int) (from & 0x00000000000000ffl);
        int byte1 = (int) ((from & 0x000000000000ff00l) >>> 8);
        int byte2 = (int) ((from & 0x0000000000ff0000l) >>> 16);
        int byte3 = (int) ((from & 0x00000000ff000000l) >>> 24);
        int byte4 = (int) ((from & 0x000000ff00000000l) >>> 32);
        int byte5 = (int) ((from & 0x0000ff0000000000l) >>> 40);
        int byte6 = (int) ((from & 0x00ff000000000000l) >>> 48);
        int byte7 = (int) ((from & 0xff00000000000000l) >>> 56);
        return (long) ((byte0 << 56) + (byte1 << 48) + (byte2 << 40) + (byte3 << 32)
                       + (byte4 << 24) + (byte5 << 16) + (byte6 << 8) + byte7);
    }
}
